﻿//===========================[ 8 / 4 / 2011 LVI ]==========================
//				Copyright c Vitaly Lyaschenko <SkyChameleon@gmail.com>
//								Source file
// Purpose: 
//=========================================================================
#include "cImage.h"
#include "cwLogger.h"
#include "cFileManager.h"

#include<IL/il.h>
#include<IL/ilu.h>
#include<IL/ilut.h>

cImage::cImage() :
    m_width( 0 ),
    m_height( 0 ),
    m_RawData( NULL )
{   }

cImage::cImage( const std::string &fileName ) :
    m_width( 0 ),
    m_height( 0 ),
    m_RawData( NULL )
{
    loadImage( fileName );
}

cImage::~cImage()
{
    if ( m_RawData != NULL )
    {
        SAFE_DELETE_ARRAY( m_RawData );
        m_RawData = NULL;
    }
}

void cImage::loadTga( const std::string &fileName )
{
    if ( fileName.empty() )
    {
        cwLogger::writeERROR( " empty file name image to load!" );
        return;
    }

    //ilEnable(IL_ORIGIN_SET);
    //ilOriginFunc(IL_ORIGIN_LOWER_LEFT);

    //ilEnable(IL_TYPE_SET);
    //ilTypeFunc(IL_UNSIGNED_BYTE);
    ilEnable(IL_FORMAT_SET);
    ilFormatFunc(IL_RGB);

    if ( !ilLoadImage( fileName.c_str() ) )
    {
        ILenum Error;
        char* errStr;
        while ((Error = ilGetError()) != IL_NO_ERROR)
        {
            errStr = const_cast<char *>( iluErrorString(Error) );
            cwLogger::writeERROR(" DevIL : "+std::string(errStr) );
        }
        return;
    }

    m_width  = ilGetInteger( IL_IMAGE_WIDTH );
    m_height = ilGetInteger( IL_IMAGE_HEIGHT );
    m_format = ilGetInteger( IL_IMAGE_FORMAT );
    m_type   = ilGetInteger( IL_IMAGE_TYPE );
    m_depth  = ilGetInteger( IL_IMAGE_DEPTH );
    m_bpp    = ilGetInteger( IL_IMAGE_BPP );

    if ( m_bpp != 3 )
    {
        cwLogger::writeERROR( "ERROR 'bytesPerPixel != 3' in : " + std::string(fileName) + " file image !" );
        return;
    }
    m_RawData = new cPyxel[m_width * m_height];
    ilCopyPixels( 0, 0, 0, m_width, m_height, m_depth, m_format, m_type, m_RawData );
}

void cImage::loadDDS( const std::string &fileName )
{
    if ( fileName.empty() )
    {
        cwLogger::writeERROR( " empty file name image to load!" );
        return;
    }

    //ilEnable(IL_ORIGIN_SET);
    //ilSetInteger(IL_ORIGIN_MODE, IL_ORIGIN_UPPER_LEFT);
    //ilutEnable(ILUT_GL_USE_S3TC);
    //ilutEnable(ILUT_GL_GEN_S3TC);
    //ilEnable(IL_TYPE_SET);
    //ilTypeFunc(IL_UNSIGNED_BYTE);
    ilEnable(IL_FORMAT_SET);
    ilFormatFunc(IL_RGB);//!

    if ( !ilLoadImage( fileName.c_str() ) )
    {
        ILenum Error;
        char* errStr;
        while ((Error = ilGetError()) != IL_NO_ERROR)
        {
            errStr = const_cast<char *>( iluErrorString(Error) );
            cwLogger::writeERROR(" DevIL : "+std::string(errStr) );
        }
        return;
    }

    m_width  = ilGetInteger( IL_IMAGE_WIDTH );
    m_height = ilGetInteger( IL_IMAGE_HEIGHT );
    m_format = ilGetInteger( IL_IMAGE_FORMAT );
    m_type   = ilGetInteger( IL_IMAGE_TYPE );
    m_depth  = ilGetInteger( IL_IMAGE_DEPTH );
    m_bpp    = ilGetInteger( IL_IMAGE_BPP );

    m_RawData = new cPyxel[m_width * m_height];
    ilCopyPixels( 0, 0, 0, m_width, m_height, m_depth, m_format, m_type, m_RawData );
}

void cImage::loadImage( const std::string &filePatch )
{
    std::string extension = cFileManager::extractFileExtension( filePatch );

    if ( ( extension == "tga" ) || ( extension == "TGA" ) )
    {
        loadTga( filePatch );
    }
    if ( ( extension == "dds" ) || ( extension == "DDS" ) )
    {
        loadDDS( filePatch );
    }
}

bool cImage::saveImage( const std::string &fileName )
{
    return true;
}